package org.mall.modules.order.controller.users;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mall.common.api.vo.CommonResult;
import org.mall.common.system.util.JwtUtil;
import org.mall.modules.order.mapper.OrderMapper;
import org.mall.modules.order.service.OrderCommentReplyService;
import org.mall.modules.order.service.OrderCommentService;
import org.mall.modules.pojo.order.bo.OrderCommentInfoAndMerchantReplyBO;
import org.mall.modules.pojo.order.bo.OrderCommentPageBO;
import org.mall.modules.pojo.order.bo.OrderCommentStateInfoPageBO;
import org.mall.modules.pojo.order.dto.CommentListCreateDTO;
import org.mall.modules.pojo.order.dto.OrderCommentCreateDTO;
import org.mall.modules.pojo.order.dto.OrderCommentPageDTO;
import org.mall.modules.pojo.order.dto.OrderCommentStateInfoPageDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

import static org.mall.common.api.vo.CommonResult.error;
import static org.mall.common.api.vo.CommonResult.success;


/**
 *
 * 订单评论 Api(user)
 *
 * @author wtz
 * @time 2019-05-27 20:46
 */
@RestController
@RequestMapping( "/user/orderComment")
@Api("用户评论模块")
public class OrderCommentController {

    @Autowired
    private OrderCommentService orderCommentService;

    @Autowired
    private OrderCommentReplyService orderCommentReplyService;
    @Autowired
    private OrderMapper orderMapper;


    @PostMapping("/createOrderComment")
    //@RequiresLogin
    @ApiOperation(value = "创建订单评论")
    public CommonResult<Boolean> createOrderComment(@RequestBody @Valid CommentListCreateDTO listCreateDTO) {
        orderMapper.selectById(12L);
        List<OrderCommentCreateDTO> listCreateDTOList=listCreateDTO.getList();
        listCreateDTOList.stream().forEach(o->{o.setUserId(JwtUtil.getUserIdByToken());});
        if (listCreateDTOList.size()<1||listCreateDTOList.get(0).getOrderId()==null){
            return error("评论参数不全");
        }
        return success(orderCommentService.createOrderComment(listCreateDTOList));
    }

    @GetMapping("/orderCommentPage")
    @ApiOperation(value = "获取评论分页")
    public CommonResult<OrderCommentPageBO> getOrderCommentPage(@Valid OrderCommentPageDTO orderCommentPageDTO){
        return success(orderCommentService.getOrderCommentPage(orderCommentPageDTO));
    }

    @GetMapping("/orderCommentInfoMerchantReply")
    @ApiOperation(value = "获取评论和商家回复")
    public CommonResult<OrderCommentInfoAndMerchantReplyBO> geOrderCommentInfoAndMerchantReply(@RequestParam("commentId") Integer commentId){
        OrderCommentInfoAndMerchantReplyBO orderCommentInfoAndMerchantReplyBO=new OrderCommentInfoAndMerchantReplyBO();
        orderCommentInfoAndMerchantReplyBO.setOrderCommentInfoBO(orderCommentService.getOrderCommentInfo(commentId));
        orderCommentInfoAndMerchantReplyBO.setOrderCommentMerchantReplyBOS(orderCommentReplyService.getOrderCommentMerchantReply(commentId));
        return success(orderCommentInfoAndMerchantReplyBO);
    }

    @GetMapping
    //@RequiresLogin
    @ApiOperation(value = "获取订单评论状态分页")
    public CommonResult<OrderCommentStateInfoPageBO> getOrderCommentStateInfoPage(@Valid OrderCommentStateInfoPageDTO orderCommentStateInfoPageDTO){
        //Integer userId = UserSecurityContextHolder.getContext().getUserId();
        //orderCommentStateInfoPageDTO.setUserId(userId);
        return success(orderCommentService.getOrderCommentStateInfoPage(orderCommentStateInfoPageDTO));
    }


}
